// Data Base
// (c) jimon game studio

#include "CDataBase.h"
#include "CBlobBuffer.h"
#include "IVariables.h"

namespace je
{
	namespace core
	{
		//! Constructor
		CDataBase_Query::CDataBase_Query(CppSQLite3Query query)
			:Query(query)
		{
		}

		//! Destructor
		CDataBase_Query::~CDataBase_Query()
		{
			Query.finalize();
		}

		//! Finalize
		void CDataBase_Query::Finalize()
		{
			Query.finalize();
		}

		//! Eof
		u1 CDataBase_Query::Eof()
		{
			return Query.eof();
		}

		//! Next Row
		void CDataBase_Query::NextRow()
		{
			Query.nextRow();
		}

		//! Return Int Field
		s32 CDataBase_Query::GetFields32(const jeStringc & FieldName,s32 DefaultValue)
		{
			return Query.getIntField(FieldName.c_str(),DefaultValue);
		}

		//! Return Int Field
		s32 CDataBase_Query::GetFields32(s32 Index,s32 DefaultValue)
		{
			return Query.getIntField(Index,DefaultValue);
		}

		//! Return Float Field
		f32 CDataBase_Query::GetFieldf32(const jeStringc & FieldName,f32 DefaultValue)
		{
			return (f32)Query.getFloatField(FieldName.c_str(),DefaultValue);
		}

		//! Return Float Field
		f32 CDataBase_Query::GetFieldf32(s32 Index,f32 DefaultValue)
		{
			return (f32)Query.getFloatField(Index,DefaultValue);
		}

		//! Return Boolean Field
		u1 CDataBase_Query::GetFieldu1(const jeStringc & FieldName,u1 DefaultValue)
		{
			return GetFields32(FieldName,DefaultValue) ? true:false;
		}

		//! Return Boolean Field
		u1 CDataBase_Query::GetFieldu1(s32 Index,u1 DefaultValue)
		{
			return GetFields32(Index,DefaultValue) ? true:false;
		}

		//! Return jeStringc Field
		jeStringc CDataBase_Query::GetFieldjeStringc(const jeStringc & FieldName,const jeStringc & DefaultValue)
		{
			return jeStringc(Query.getStringField(FieldName.c_str(),DefaultValue.c_str()));
		}

		//! Return jeStringc Field
		jeStringc CDataBase_Query::GetFieldjeStringc(s32 Index,const jeStringc & DefaultValue)
		{
			return jeStringc(Query.getStringField(Index,DefaultValue.c_str()));
		}

		//! Return Field Binary Data
		jeMemoryBuffer * CDataBase_Query::GetFieldBuffer(const jeStringc & FieldName)
		{
			s32 Size = 0;
			const u8 * Data = Query.getBlobField(FieldName.c_str(),Size);
			if(!Data)
				return NULL;
			jeMemoryBuffer * Buffer;
			JENEW(Buffer,jeMemoryBuffer)
			Buffer->Lock(BLT_WRITE);
			Buffer->Allocate(Size);
			Buffer->Write(Data,Size);
			Buffer->UnLock();
			return Buffer;
		}

		//! Return Field Binary Data
		jeMemoryBuffer * CDataBase_Query::GetFieldBuffer(s32 Index)
		{
			s32 Size = 0;
			const u8 * Data = Query.getBlobField(Index,Size);
			jeMemoryBuffer * Buffer;
			JENEW(Buffer,jeMemoryBuffer)
			Buffer->Lock(BLT_WRITE);
			Buffer->Allocate(Size);
			Buffer->Write(Data,Size);
			Buffer->UnLock();
			return Buffer;
		}

		//! Is Field Exist
		u1 CDataBase_Query::IsFieldExist(const jeStringc & FieldName)
		{
			return !Query.fieldIsNull(FieldName.c_str());
		}

		//! Constructor
		CDataBase::CDataBase(const jeStringc & FileName)
		{
			JENEW(DataBase,CppSQLite3DB)
			DataBase->open((jeStringc(GetVariables()->GetVariableString("Enesea_WorkingDirectory").c_str())+FileName).c_str());
		}

		//! Destructor
		CDataBase::~CDataBase()
		{
			DataBase->close();
			JEDELETE(DataBase)
		}

		//! Query
		IDataBase_Query * CDataBase::Query(const jeStringc & Query)
		{
			IDataBase_Query * DBQuery = NULL;
			JENEW(DBQuery,CDataBase_Query(DataBase->execQuery(Query.c_str())))
			return DBQuery;
		}

		//! Return Field Binary Data With Streaming
		IBlobBuffer * CDataBase::GetBlobBuffer(const jeStringc & Table,const jeStringc & FieldName,const s32 RowId)
		{
			sqlite3_blob * OpenBlob = NULL;
			sqlite3_blob_open(DataBase->GetDB(),"main",Table.c_str(),FieldName.c_str(),(sqlite3_int64)RowId,1,&OpenBlob);

			IBlobBuffer * Buffer = NULL;
			JENEW(Buffer,CBlobBuffer(OpenBlob))
			return Buffer;

		}

		//! Create Data Base
		IDataBase * JE_ENGINE_FUNCTION_CALL CreateDataBase(const jeStringc & FileName)
		{
			IDataBase * DB = NULL;
			JENEW(DB,CDataBase(FileName))
			return DB;
		}
	}
}
